/**
 * author:Administrator
 * date:2015/7/27
 * desc:TODO
 */

'use strict';

let esDao = require('./es/ArticleDao.js');

function DaoDispatcher() {
}
module.exports = new DaoDispatcher();

let theProto = DaoDispatcher.prototype;

/**
 * 增加
 {
     uid: env.I.uid,
     tid: env.I.tid,
     articleContent: env.I.articleContent
 }
 * @param json
 * @returns {*}
 */
theProto.add = function*(json) {
  let res = yield esDao.createSingle(json);
  return res;
};

theProto.addBatch = function*(list) {
  let res = yield esDao.createMany(list);
  return res;
};

theProto.deleteMapping = function*() {
  let res = yield esDao.deleteMapping();
  return res;
};

theProto.createMapping = function*() {
  let res = yield esDao.createMapping();
  return res;
};

theProto.reBuild = function*() {
  let res = yield esDao.deleteAll();
  yield esDao.createMapping();
  return res;
};

theProto.index = function*() {
  // TODO
  let res = yield esDao.getIndex();
  return res;
};

theProto.deleteDoc = function*(json) {
  let result;
  if (json && json.id !== undefined) {
    result = yield this.deleteById(json.id);
  } else {
    result = yield this.deleteAll();
  }
  return result;
};

theProto.deleteById = function*(id) {
  let res = yield esDao.deleteById(id);
  return res;
};

theProto.deleteAll = function*() {
  let res = yield esDao.deleteAll();
  yield esDao.createMapping();
  return res;
};

/**
 * 对短语进行频率统计 term = 短语, freq = frequency频率
 * @param field
 * @param topNum
 * @returns {*}
 */
theProto.termFreq = function* (field, topNum) {
  let res = yield esDao.termFreq(field, topNum);
  return res;
};


/**
 * 对一个帖子进行词汇统计
 * @param json {tid: tid, debug: debug}
 */
theProto.termVector = function*(json) {
  var fieldName = 'articleContent';
  let result = yield esDao.termVector(json.tid, fieldName, json.debug);
  return result;
};

/**
 * 快速查询 quick search
 * {q: q, size: size, from: from}
 * @param q
 * @returns {*}
 */
theProto.qSearch = function* (json) {
  let res = yield esDao.qSearch(json);
  return res;
};

/**
 {
    refer: env.I.refer,
    content: env.I.content,
    top: env.I.top
 }
 * @param json
 * @returns {*}
 */
theProto.autoCate = function* (json) {
  let res = yield esDao.autoCate(json);
  return res;
};

/**
 * 找到内容
 * @param tid
 */
theProto.getById = function*(tid) {
  let res = yield esDao.getById(tid);
  return res;
};

/**
 * 自定义 _score 查询,目前规则很简单,可以根据业务进行修改
 * _score = doc['uid'].value * mul + add - sub
 * @param json
 * @returns {*}
 */
theProto.cScore = function* (json) {
  let res = yield esDao.cScore(json);
  return res;
};

/**
 * 测试接口@ 连接池测试
 * @returns {*}
 */
theProto.cleanReq = function* () {
  let res = yield esDao.cleanReq();
  return res;
};

/**
 * 词语分析
 * @param content
 * @returns {*}
 */
theProto.analyze = function*(content) {
  let res = yield esDao.analyze(content);
  return res;
};

/**
 * 精细查询
 * @param wordList
 * @returns {*}
 */
theProto.fineSearch = function*(json) {
  let res = yield esDao.fineSearch(json);
  return res;
};

theProto.wordFilter = function*(wordList){
  let res = yield esDao.wordFilter(wordList);
  return res;
};